package com.intel.two.controller.record;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.read.builder.ExcelReaderBuilder;
import com.alibaba.excel.read.builder.ExcelReaderSheetBuilder;
import com.intel.two.common.LayuiPageVo;
import com.intel.two.common.R;
import com.intel.two.pojo.record.RentingaHouseRecord;
import com.intel.two.pojo.record.listener.RrecordListener;
import com.intel.two.pojo.record.recordbo.RentingaHouseRecordBo;
import com.intel.two.service.record.RentingaHouseRecordService;
import org.apache.commons.io.FileUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.Map;

@RestController
@RequestMapping("/rentingahouse")
public class RentingaHouseRecordController {
    @Autowired
    private RentingaHouseRecordService rentingaHouseRecordService;
    @RequestMapping("/rentingahouseList")
    public LayuiPageVo<RentingaHouseRecord> rentingahouseList(@RequestParam Map<String,Object> params){
        return rentingaHouseRecordService.rentingahouseList(params);
    }
    @PostMapping("/rentingahouseAdd")
    public R rentingahouseAdd(@RequestBody RentingaHouseRecord record){
        int i=rentingaHouseRecordService.rentingahouseAdd(record);
        return i!=0?R.ok():R.error();
    }
    @GetMapping("/deleteRentingahouse")
    public  R deleteRentingahouse(Integer id){
        int i=rentingaHouseRecordService.deleteRentingahouse(id);
        return i!=0?R.ok():R.error();
    }
    @PostMapping("/deleteRentingahouseList")
    public R deleteRentingahouseList(String ids){
        int i=rentingaHouseRecordService.deleteRentingahouseList(ids);
        return i!=0?R.ok():R.error();
    }
    @RequestMapping("/update")
    public R update(RentingaHouseRecord record){
        int i=rentingaHouseRecordService.update(record);
        return i!=0?R.ok():R.error();
    }
    @RequestMapping("/templateDownload")
    public void templateDownload(HttpServletRequest request, HttpServletResponse response) throws IOException {
        //下载 excel文件设置响应头
        response.setContentType("application/vnd.ms-excel");
        response.setCharacterEncoding("utf-8");
        //获取模板路径
        String realPath = request.getServletContext().getRealPath("/template/居民租房模板档案.xlsx");

        //根据文件的真实路径 得到文件对象
        File file = new File(realPath);
        //将文件对象转为字节数组
        byte[] bytes = FileUtils.readFileToByteArray(file);

        //把模板下载到前端
        String filename = URLEncoder.encode("居民租房模板档案", "utf-8");//防止中文乱码
        //通过这个响应头告诉浏览器 下载的文件名字是什么
        response.setHeader("content-Disposition","attachment;filename="+filename+".xlsx");
        //得到指向浏览器的输出流
        ServletOutputStream outputStream = response.getOutputStream();

        outputStream.write(bytes);
        outputStream.close();
    }
    @Autowired
    RrecordListener rrecordListener;
    //读取excel文件 把excel中的数据存入数据库
    @RequestMapping("/import")
    public R importExcelOne(MultipartFile file) throws IOException {
        /**
         * 参数一：文件流
         * 参数二：读取到的数据封装到哪个类型
         * 参数三：监听器 每读取一行数据会执行invoke方法
         */
        ExcelReaderBuilder read= EasyExcel.read(file.getInputStream(), RentingaHouseRecordBo.class,rrecordListener);
        //得到读取的sheet页对象
        ExcelReaderSheetBuilder sheet = read.sheet();
        //读取sheet
        sheet.doRead();
        return R.ok();
    }
}
